package com.rain.shoppingscienceproj.ui.activity.order;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.http.BaseSubscriber;
import com.rain.shoppingscienceproj.http.ExceptionHandle;
import com.rain.shoppingscienceproj.http.RetrofitClient;
import com.rain.shoppingscienceproj.module.OrderBetweenDto;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.adapter.OrderGoodsAdapter;
import com.rain.shoppingscienceproj.ui.base.GlobalData;
import com.rain.shoppingscienceproj.ui.utile.TitleUtils;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.ImageUtils;
import com.rain.shoppingscienceproj.utils.LoadingDialog;
import com.rain.shoppingscienceproj.utils.layoutmanager.CustomLinearLayoutManager;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailsActivity extends BaseActivity {

    private String orderId;

    @BindView(R.id.tv_status)
    TextView tv_status;

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_address_mobile)
    TextView tv_address_mobile;
    @BindView(R.id.tv_address_name)
    TextView tv_address_name;
    @BindView(R.id.tv_add_address)
    TextView tv_add_address;
    @BindView(R.id.ll_address)
    LinearLayout ll_address;
    @BindView(R.id.tv_money_price)
    TextView tv_money_price;
    @BindView(R.id.ll_order_info)
    LinearLayout ll_order_info;
    @BindView(R.id.tv_order_number)
    TextView tv_order_number;
    @BindView(R.id.tv_apply_type)
    TextView tv_apply_type;
    @BindView(R.id.tv_order_time)
    TextView tv_order_time;
    @BindView(R.id.ll_wuliu)
    LinearLayout ll_wuliu;
    @BindView(R.id.tv_wuliu)
    TextView tv_wuliu;

    @BindView(R.id.tv_store_name)
    TextView tv_store_name;
    @BindView(R.id.iv_store_image)
    ImageView iv_store_image;

    @OnClick(R.id.ll_wuliu)
    void wuliu() {
        ToastUtils.showShortToast("=======");
    }

    private OrderGoodsAdapter makeOrderAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        loadingDialog = new LoadingDialog(this, "正在加载...");
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        TitleUtils.TitleView(this, "订单详情");
        userInfo = GlobalData.getUserInfo();
        orderId = getIntent().getStringExtra("orderId");
        mRecyclerView.setLayoutManager(new CustomLinearLayoutManager(this));
        makeOrderAdapter = new OrderGoodsAdapter(this);
        mRecyclerView.setAdapter(makeOrderAdapter);
        ll_order_info.setVisibility(View.VISIBLE);
        initData();
    }

    private void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("userId", userInfo.getUserId());
        map.put("token", userInfo.getToken());
        map.put("orderBetweenId", orderId);
        RetrofitClient.getInstance(this)
                .createBaseApi()
                .orderdetails(map, new BaseSubscriber<OrderBetweenDto>(this) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        ToastUtils.showShortToast(e.Message);
                    }

                    @Override
                    public void onNext(OrderBetweenDto myCarData) {
                        super.onNext(myCarData);
                        tv_address_mobile.setText("收货人 : " + myCarData.getUsername() + "\t\t\t\t\t\t\t\t\t " + myCarData.getUserphone());
                        tv_address_name.setText("收货地址 : " + myCarData.getUseraddr());
                        makeOrderAdapter.setList(myCarData.getShoppingcarDtos());
                        tv_money_price.setText("¥ " + (myCarData.getTotalpay() / 100));
                        tv_store_name.setText(myCarData.getShop().getShopName());
                        ImageUtils.image(OrderDetailsActivity.this, myCarData.getShop().getShopImage(), iv_store_image);
                        if (myCarData.getStatus() == 0) {
                            tv_status.setText("订单状态： 未支付");
                            ll_wuliu.setVisibility(View.GONE);
                            ll_order_info.setVisibility(View.GONE);
                        } else if (myCarData.getStatus() == 1) {
                            tv_status.setText("订单状态： 代发货");
                            ll_wuliu.setVisibility(View.GONE);
                            initOrder(myCarData);
                        } else if (myCarData.getStatus() == 2) {
                            tv_status.setText("订单状态： 已发货");
                            ll_wuliu.setVisibility(View.VISIBLE);
                            tv_wuliu.setText("查看物流");
                            initOrder(myCarData);
                        } else if (myCarData.getStatus() == 3) {
                            tv_status.setText("订单状态： 已收货");
                            ll_wuliu.setVisibility(View.GONE);
                            initOrder(myCarData);
                        } else {
                            tv_status.setText("订单状态： 已完成");
                            ll_wuliu.setVisibility(View.GONE);
                            initOrder(myCarData);
                        }
                    }
                });
    }

    private void initOrder(OrderBetweenDto myCarData) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(myCarData.getCtime());
        tv_order_number.setText(myCarData.getOrderid());
        tv_order_time.setText(dateString);
        if (myCarData.getPayType().equals("1")) {
            tv_apply_type.setText("微信支付");
        } else {
            tv_apply_type.setText("支付宝支付");
        }
    }
}
